package com.example.klaraservice.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.example.klaraservice.dao.OrdersMapper;
import com.example.klaraservice.pojo.OrderItem;
import com.example.klaraservice.pojo.Orders;
import com.example.klaraservice.service.OrdersService;
import com.example.klaraservice.utils.UrlUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author : Glacialerisa
 * @date : 2023/6/13 22:37
 * @desc :
 */
@Service
public class OrdersServiceImpl implements OrdersService {
    @Autowired
    private OrdersMapper ordersMapper;

    @Override
    public boolean addOrder(Orders orders,int userId) {
        //订单编号生成
        String date = DateUtil.today();
        String time = DateUtil.format(DateUtil.date(), "HHmmssSSS");
        String uuid = IdUtil.simpleUUID();
        String result = date + time + uuid.substring(uuid.length() - 6);
        orders.setOrdersNumber(result);
        return ordersMapper.addOrder(orders, userId);
    }

    @Override
    public List<Orders> getUserOrders(int userId) {
        List<Orders> orders = ordersMapper.selectOrdersByUserId(userId);
        for (Orders o: orders
             ) {
            for (OrderItem ot:o.getOrderItemList()
                 ) {
                String image = ot.getImage();
                ot.setImage(UrlUtil.IP+image);
            }
        }
        return orders;
    }

    @Override
    public Orders selectByOrdersNumber(String ordersNumber) {
        Orders orders = ordersMapper.selectByOrdersNumber(ordersNumber);
        for (OrderItem ot:orders.getOrderItemList()
             ) {
            String image = ot.getImage();
            ot.setImage(UrlUtil.IP+image);
        }
        return orders;
    }

    @Override
    public boolean updateOrdersInfo(Orders orders) {
        return ordersMapper.updateOrder(orders);
    }
}
